package com.handpay.nocardpay.baselibrary.bankmanager.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.bankmanager.presenter.BankInfoPresenterImpl;
import com.handpay.nocardpay.baselibrary.bankmanager.view.ISubBankView;
import com.handpay.nocardpay.baselibrary.qualificationinfo.adapters.SubBankAdapter;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.BankList;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.SubBankBean;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by sxshi on 2017/8/15.
 */

public class SubBankSearchFragment extends BaseFragment implements ISubBankView {
    private EditText mSubKeyWord;
    private TextView mEmptyView;
    private ListView mSubList;

    private SubBankAdapter subBankAdapter;
    private List<SubBankBean.ListBean> mLists;

    private BankInfoPresenterImpl bankInfoPresenter;

    //支行查询一页条数
    public static String PAGESIZE = "30";
    //接收的主行信息
    private BankList.ListBean mMainBank;

    public static SubBankSearchFragment newInstance(BankList.ListBean bank) {

        Bundle args = new Bundle();
        args.putParcelable("bank", bank);
        SubBankSearchFragment fragment = new SubBankSearchFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_subbanklist;
    }

    @Override
    public String getFragmentTitle() {
        return getString(R.string.select_subbank);
    }

    @Override
    public void initView(View view) {
        Toolbar mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolbar(mToolbar, true);
        mSubKeyWord = (EditText) view.findViewById(R.id.et_head);
        mEmptyView = (TextView) view.findViewById(R.id.tv_empty);
        mSubList = (ListView) view.findViewById(R.id.lv_content);
    }

    @Override
    public void initListener() {
        mSubKeyWord.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (null == mMainBank || "-1".equals(mMainBank.getBankCode())) {
                    String title = _mActivity.getString(R.string.tip);
                    String message = _mActivity.getString(R.string.select_bank_tip);
                    DialogUtils.showAlertDialog(_mActivity, title, message, true);
                } else {
                    String keyword = s.toString();
                    querySubBankList(mMainBank.getBankCode(), keyword);
                }
            }
        });


        mSubList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //返回点击的支行信息
                Bundle bundle = new Bundle();
                bundle.putParcelable("subBank", mLists.get(position));
                setFragmentResult(RESULT_OK, bundle);
                pop();
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        bankInfoPresenter = new BankInfoPresenterImpl(_mActivity);
        bankInfoPresenter.setiSubBankView(this);

        mMainBank = getArguments().getParcelable("bank");
        subBankAdapter = new SubBankAdapter(_mActivity);
        mLists = new ArrayList<>();
        mSubList.setAdapter(subBankAdapter);
        if (mLists.isEmpty()) {
            mSubList.setEmptyView(mEmptyView);
        }
    }


    @Override
    public void showSubBankList(SubBankBean subBankBean) {
        if (!subBankBean.getList().isEmpty()) {
            mLists = subBankBean.getList();
            subBankAdapter.setmLists(mLists);
        }
    }

    /**
     * 查询支行列表
     *
     * @param bankCode 银行编码
     * @param text     关键字
     */
    private void querySubBankList(String bankCode, String text) {
        HashMap<String, Object> paramers = new HashMap<String, Object>();
        paramers.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
        paramers.put(BaseProtocolKey.BANKCODE, String.valueOf(bankCode));
        paramers.put(BaseProtocolKey.SEARCHTEXT, text);
        paramers.put(BaseProtocolKey.PAGESIZE, PAGESIZE);
        bankInfoPresenter.querySubBankList(paramers, SubBankBean.class, false);
    }
}
